package com.zjj.edu.web.controller.kfzx;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.system.domain.SysStudent;
import com.zjj.edu.system.service.ISysStudentService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/kfzx/student")
public class StudentController extends BaseController {

    private String prefix = "kfzx/student";

    @Autowired
    private ISysStudentService studentService;
    
    @Autowired
    private ISysTenantService tenantService;

    @RequiresPermissions("kfzx:student:view")
    @GetMapping()
    public String student(ModelMap mmap) {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/student";
    }

    /**
     * 查询学员列表
     */
    @RequiresPermissions("kfzx:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStudent student) {
        startPage();
        student.setTenantId(ShiroUtils.getTenantId());
        List<SysStudent> list = studentService.selectSysStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学员列表
     */
    @RequiresPermissions("kfzx:student:export")
    @Log(title = "学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStudent student) {
        student.setTenantId(ShiroUtils.getTenantId());
        List<SysStudent> list = studentService.selectSysStudentList(student);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 新增学员
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存学员
     */
    @Log(title = "学员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStudent student) {
        student.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(studentService.checkNameUnique(student))) {
            return error("新增学员'" + student.getName() + "'失败，名称已存在");
        }
        student.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(studentService.insertSysStudent(student));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkNameUnique(SysStudent student) {
        student.setTenantId(ShiroUtils.getTenantId());
        return studentService.checkNameUnique(student);
    }

    /**
     * 修改学员
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        long tenantId = ShiroUtils.getTenantId();
        mmap.put("student", studentService.selectSysStudentById(id, tenantId));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存学员
     */
    @RequiresPermissions("kfzx:student:edit")
    @Log(title = "学员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStudent student) {
        student.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(studentService.checkNameUnique(student))) {
            return error("修改学员'" + student.getName() + "'失败，名称已存在");
        }
        student.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(studentService.updateSysStudent(student));
    }

    /**
     * 状态修改
     */
    @Log(title = "学员管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kfzx:student:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysStudent student) {
        student.setTenantId(ShiroUtils.getTenantId());
        return toAjax(studentService.updateSysStudent(student));
    }

}